package com.example.administrator.studywechatactivity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.studywechatactivity.WechatFragment;
import com.example.administrator.studywechatactivity.MeFragment;
import com.example.administrator.studywechatactivity.FindFragment;
import com.example.administrator.studywechatactivity.ContactFragment;



public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    //底部菜单栏3个TextView
    private TextView mTextWechat;
    private TextView mTextContact;
    private TextView mTextFind;
    private TextView mTextMe;

    //3个Fragment

    private Fragment mWechatFragment;
    private Fragment mContactFragment;
    private Fragment mFindFragment;
    private Fragment mMeFragment;

    //标记当前显示的Fragment
    private int fragmentId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        init();
        //根据传入的Bundle对象判断Activity是正常启动还是销毁重建
        if(savedInstanceState == null){
            //设置第一个Fragment默认选中
            setFragment(0);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //通过onSaveInstanceState方法保存当前显示的fragment
        outState.putInt("fragment_id",fragmentId);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        FragmentManager mFragmentManager = getSupportFragmentManager();
        //通过FragmentManager获取保存在FragmentTransaction中的Fragment实例
        mWechatFragment = (WechatFragment)mFragmentManager
                .findFragmentByTag("clothes_fragment");
        mContactFragment = (ContactFragment)mFragmentManager
                .findFragmentByTag("food_fragment");
        mFindFragment = (FindFragment)mFragmentManager
                .findFragmentByTag("hotel_fragment");
        mMeFragment = (MeFragment)mFragmentManager
                .findFragmentByTag("me_fragment");
        //恢复销毁前显示的Fragment
        setFragment(savedInstanceState.getInt("fragment_id"));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.text_wechat:
                setFragment(0);
                break;
            case R.id.text_contact:
                setFragment(1);
                break;
            case R.id.text_find:
                setFragment(2);
                break;
            case R.id.text_me:
                setFragment(3);
                break;
            default:
                break;
        }
    }

    private void init(){
        //初始化控件
        mTextWechat = (TextView)findViewById(R.id.text_wechat);
        mTextContact = (TextView)findViewById(R.id.text_contact);
        mTextFind = (TextView)findViewById(R.id.text_find);
        mTextMe = (TextView)findViewById(R.id.text_me);

        //设置监听
        mTextWechat.setOnClickListener(this);
        mTextContact.setOnClickListener(this);
        mTextFind.setOnClickListener(this);
        mTextMe.setOnClickListener(this);
    }

    private void setFragment(int index){
        //获取Fragment管理器
        FragmentManager mFragmentManager = getSupportFragmentManager();
        //开启事务
        FragmentTransaction mTransaction = mFragmentManager.beginTransaction();
        //隐藏所有Fragment
        hideFragments(mTransaction);
        switch (index){
            default:
                break;
            case 0:
                fragmentId = 0;
                //设置菜单栏为选中状态（修改文字和图片颜色）
                mTextWechat.setTextColor(getResources()
                        .getColor(R.color.colorTextPressed));
                mTextWechat.setCompoundDrawablesWithIntrinsicBounds(0,
                        R.drawable.menu_msg_selected,0,0);
                //显示对应Fragment
                if(mWechatFragment == null){
                    mWechatFragment = new WechatFragment();
                    mTransaction.add(R.id.action_container, mWechatFragment,
                            "clothes_fragment");
                }else {
                    mTransaction.show(mWechatFragment);
                }
                break;
            case 1:
                fragmentId = 1;
                mTextContact.setTextColor(getResources()
                        .getColor(R.color.colorTextPressed));
                mTextContact.setCompoundDrawablesWithIntrinsicBounds(0,
                        R.drawable.menu_contact_selected,0,0);
                if(mContactFragment == null){
                    mContactFragment = new ContactFragment();
                    mTransaction.add(R.id.action_container, mContactFragment,
                            "food_fragment");
                }else {
                    mTransaction.show(mContactFragment);
                }
                break;
            case 2:
                fragmentId = 2;
                mTextFind.setTextColor(getResources()
                        .getColor(R.color.colorTextPressed));
                mTextFind.setCompoundDrawablesWithIntrinsicBounds(0,
                        R.drawable.menu_find_selected,0,0);
                if(mFindFragment == null){
                    mFindFragment = new FindFragment();
                    mTransaction.add(R.id.action_container,mFindFragment,
                            "hotel_fragment");
                }else {
                    mTransaction.show(mFindFragment);
                }
                break;
            case 3:
                fragmentId = 3;
                mTextMe.setTextColor(getResources()
                        .getColor(R.color.colorTextPressed));
                mTextMe.setCompoundDrawablesWithIntrinsicBounds(0,
                        R.drawable.menu_me_selected,0,0);
                if(mMeFragment == null){
                    mMeFragment = new MeFragment();
                    mTransaction.add(R.id.action_container, mMeFragment,
                            "hotel_fragment");
                }else {
                    mTransaction.show(mMeFragment);
                }
                break;
        }
        //提交事务
        mTransaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction){
        if(mWechatFragment != null){
            //隐藏Fragment
            transaction.hide(mWechatFragment);
            //将对应菜单栏设置为默认状态
            mTextWechat.setTextColor(getResources()
                    .getColor(R.color.colorText));
            mTextWechat.setCompoundDrawablesWithIntrinsicBounds(0,
                    R.drawable.menu_msg_default,0,0);
        }
        if(mContactFragment != null){
            transaction.hide(mContactFragment);
            mTextContact.setTextColor(getResources()
                    .getColor(R.color.colorText));
            mTextContact.setCompoundDrawablesWithIntrinsicBounds(0,
                    R.drawable.menu_contact_default,0,0);
        }
        if(mFindFragment != null){
            transaction.hide(mFindFragment);
            mTextFind.setTextColor(getResources()
                    .getColor(R.color.colorText));
            mTextFind.setCompoundDrawablesWithIntrinsicBounds(0,
                    R.drawable.menu_find_default,0,0);
        }
        if(mMeFragment != null){
            transaction.hide(mMeFragment);
            mTextMe.setTextColor(getResources()
                    .getColor(R.color.colorText));
            mTextMe.setCompoundDrawablesWithIntrinsicBounds(0,
                    R.drawable.menu_me_default,0,0);
        }
    }
}
